[Music]
Welcome to Extending Node.js. After watching this video, you should be able
to: Extend Node.js with third-party packages and
define package dependency. You can build a hypertext transfer protocol
(HTTP) server using Node.js, but the default Node.js framework is limited. However, there are many aspects of building
a web server like routing, parsing incoming files, authentication, connecting to a database,
and other aspects. For example, you can parse extensible markup
language (XML) with JavaScript string functions, but an XML document object is not efficient
in parsing a stream of XML data. Developers rely on external libraries and
packages to extend the core Node.js functionality. Say you need to know about the weather data
of San Francisco International Airport. You can send an HTTP request to an external
web server. After you receive the weather data, you can
parse it manually. Here you parse web service messages as a string. When Node.js receives the final part of the
HTTP response message, it calls the response.on('end') event handler. Inside the callback function, the first line
with the buffer.match function call locates the part of the XML message that begins and
ends with the temp_f XML element tag. This string, including the temp_f start and
end tags, is saved in the matches variable. If there is a match, the second call to matches[0].replace
strips out the start and end temp_f XML elements. Consider the disadvantages of treating XML
data as a string. String matching ignores the structure of XML
data. The message body might contain malformed XML
data. Depending on the complexity of the XML data,
string matching might be more efficient than building an XML tree of the data. String matching is less tolerant to changes
in the XML data structure. And if the message adds or removes any XML
elements, you must change the regular expression on the string match function. You can use the xml2js Node.js package to
parse a string of XML elements into a JavaScript object. This is a Node.js package that uses only JavaScript. It does not require an XML parsing library
written in another language. Because third-party packages licenses might
have their own licenses, confirm that the licensing terms work with your company and
your application before installing the xml2js Node.js package. You can use the npm application to manage
Node.js packages in your Node.js framework installation. For instance, run the npm install command
to retrieve and set up a Node.js package and any package dependencies. In this example, you call npm install xml2js
from the command line. The npm application goes out to the Internet
and retrieves the Node.js module xml2js and any dependencies that it requires. Here, you import the package into your application. In the first line, the parseString function
calls the callback function when it finishes processing the XML tree in buffer. In the last line, the result JavaScript variable
represents the contents of the XML fragment in buffer. In other words, the result JavaScript variable
represents the XML element, but in JavaScript form. The property current_observation.temp_f[0]
maps to the first child element in the temp_f XML element. The temp_f element is a child of the current_observation
element. In this video, you learned that: Developers rely on third-party packages to
extend Node.js's features. String matching ignores the structure of XML
data. The xml2js Node.js package parses a string
of XML elements into a JavaScript object and the npm application manages Node.js packages
in your Node.js framework installation.